package com.chw.common.kuyu;

import com.chw.common.diy.PassExction;
import com.chw.common.diy.TokenException;
import com.chw.common.exception.ParamExcption;
import com.chw.user.entity.po.User;
import com.chw.util.JWT;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("欢迎来到帅逼奥广场");
        /* 认证的逻辑    1  判断是否有 token   2 判断token是否正确  3 token解析后  */
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            throw new TokenException("token为空");
        }
        User unsign = JWT.unsign(token, User.class);
        if (unsign==null){
            throw new PassExction();
        }
        System.out.println(token);
        //解析用户信息
        request.setAttribute("u",unsign);
        return true;
    }
}
